# -*- coding: utf-8 -*-
require 'test/unit'
require 'm3g'

class Test_MorphingMesh < Test::Unit::TestCase
  def setup
  end

  def teadown
  end

  def test_default_variables
    varry = M3G::VertexArray.new 16, 3, 2
    tris  = M3G::TriangleStripArray.new [0,1,2], [3]
    app   = M3G::Appearance.new

    base_vertices              = M3G::VertexBuffer.new
    base_vertices.positions    = varry, 1, [0,0,0]
    target_vertices1           = M3G::VertexBuffer.new
    target_vertices2           = M3G::VertexBuffer.new
    target_vertices1.positions = varry, 1, [0,0,0]
    target_vertices2.positions = varry, 1, [0,0,0]
    targets_vertices           = [target_vertices1, target_vertices2]

    mesh = M3G::MorphingMesh.new base_vertices, targets_vertices, tris, app

    assert_equal 2               , mesh.morph_target_count
    assert_equal target_vertices1, mesh.morph_target[0]
    assert_equal target_vertices2, mesh.morph_target[1]
  end

  def test_set_variables
    varry  = M3G::VertexArray.new 16, 3, 2
    tris   = M3G::TriangleStripArray.new [0,1,2], [3]
    app    = M3G::Appearance.new

    base_vertices              = M3G::VertexBuffer.new 
    base_vertices.positions    = varry, 1, [0,0,0];
    target_vertices1           = M3G::VertexBuffer.new
    target_vertices2           = M3G::VertexBuffer.new
    target_vertices1.positions = varry, 1, [0,0,0];
    target_vertices2.positions = varry, 1, [0,0,0];
    targets_vertices           = [target_vertices1, target_vertices2]
    mesh = M3G::MorphingMesh.new base_vertices, targets_vertices, tris, app

    assert_equal 2, mesh.morph_target_count
  
    mesh.weights = [1,2]
    weights = mesh.weights

    assert_equal 2  ,  weights.length
    assert_equal 1.0,  weights[0]
    assert_equal 2.0,  weights[1]
  end

  def test_duplicate
    varry  = M3G::VertexArray.new 16, 3, 2
    tris   = M3G::TriangleStripArray.new [0,1,2], [3]
    app    = M3G::Appearance.new

    base_vertices              = M3G::VertexBuffer.new 
    base_vertices.positions    = varry, 1, [0,0,0];
    target_vertices1           = M3G::VertexBuffer.new
    target_vertices2           = M3G::VertexBuffer.new
    target_vertices1.positions = varry, 1, [0,0,0];
    target_vertices2.positions = varry, 1, [0,0,0];
    targets_vertices           = [target_vertices1, target_vertices2]
    mesh1 = M3G::MorphingMesh.new base_vertices, targets_vertices, tris, app
    mesh1.weights = [1,2]

    mesh2 = mesh1.duplicate

    assert_not_equal mesh1, mesh2
    assert_equal     2    , mesh2.morph_target_count
    assert_equal     2    , mesh2.weights.length
    assert_equal     1.0  , mesh2.weights[0]
    assert_equal     2.0  , mesh2.weights[1]
  end

 def test_references
    varry  = M3G::VertexArray.new 16, 3, 2
    tris   = M3G::TriangleStripArray.new [0,1,2], [3]
    app    = M3G::Appearance.new

    base_vertices              = M3G::VertexBuffer.new 
    base_vertices.positions    = varry, 1, [0,0,0];
    target_vertices1           = M3G::VertexBuffer.new
    target_vertices2           = M3G::VertexBuffer.new
    target_vertices1.positions = varry, 1, [0,0,0];
    target_vertices2.positions = varry, 1, [0,0,0];
    targets_vertices           = [target_vertices1, target_vertices2]
    mesh = M3G::MorphingMesh.new base_vertices, targets_vertices, tris, app
    mesh.weights = [1,2]

    objs = mesh.references

    assert_equal 5                , objs.length
    assert_equal base_vertices    , objs[0]
    assert_equal tris             , objs[1]
    assert_equal app              , objs[2]
    assert_equal target_vertices1 , objs[3]
    assert_equal target_vertices2 , objs[4]
  end

end
